package com.may._5;

import java.util.concurrent.CountDownLatch;

/**
 * 枚举相当于数据库
 */
enum CountEnum{
    //这些相当于数据库中的表，每个表中只有一条记录
    ONE(0,"齐"),
    TWO(1,"楚"),
    THREE(2,"燕"),
    FOUR(3,"赵"),
    FIVE(4,"魏"),
    SIX(5,"韩");

    //这些相当于表字段名
    private Integer retCode;
    private String retMessage;

    public Integer getRetCode() {
        return retCode;
    }

    public String getRetMessage() {
        return retMessage;
    }

    CountEnum(Integer retCode, String retMessage) {
        this.retCode = retCode;
        this.retMessage = retMessage;
    }

    public static CountEnum getCountry(int index){
        CountEnum[] values = CountEnum.values();//枚举自带遍历方法
        for (CountEnum element : values){
            if (index == element.getRetCode()){
                return element;
            }
        }
        return null;
    }
}

public class CountDownLatchDemo {
    public static void main(String[] args) throws Exception {
        final int count = 6;
        CountDownLatch countDownLatch = new CountDownLatch(count);//只有有参构造器
        for (int i = 0; i < count; i++){
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+"国，被消灭了");
                countDownLatch.countDown();
            },CountEnum.getCountry(i).getRetMessage()).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"一统天下");
    }

    public static void countDown() throws InterruptedException {
        final int count = 6;
        CountDownLatch countDownLatch = new CountDownLatch(count);//只有有参构造器
        for (int i = 0; i < count; i++){
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+"执行了");
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"执行了");
    }
}
